package digest

import (
	//"bytes"
	"encoding/xml"
	"errors"
	"fmt"
	//"gipc-business/digest"
	"digest/models"
	"strings"

	//"io"
	"io/ioutil"
	"net/http"
)

func GetEZVIZ(username string, password string, address string) (*models.EZVIZ, error) {
	t := NewTransport(username, password)
	uri := "http://" + address + "/ISAPI/System/Network/EZVIZ"
	req, err := http.NewRequest("GET", uri, nil)
	if err != nil {
		return nil, err
	}
	resp, err := t.RoundTrip(req)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)

	fmt.Println(string(body))

	if resp.StatusCode == 200 {
		ezviz := models.EZVIZ{}
		err = xml.Unmarshal([]byte(body), &ezviz)
		if err != nil {
			fmt.Printf("error: %v", err)
			return nil, err
		}

		return &ezviz, nil
	} else {
		return nil, errors.New(fmt.Sprintf("Error %d", resp.StatusCode))
	}
}

func EnabledEZVIZ(username string, password string, address string, enable string) (*models.EZVIZResponseStatus, error) {
	t := NewTransport(username, password)
	uri := "http://" + address + "/ISAPI/System/Network/EZVIZ"

	payload := `<?xml version="1.0" encoding="utf-8"?>
				<EZVIZ version="2.0" xmlns="http://www.isapi.org/ver20/XMLSchema">
					<enabled>` + enable + `</enabled>
				</EZVIZ>`

	resp, err := t.Put("PUT", uri, payload)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)

	fmt.Println(string(body))

	if resp.StatusCode == 200 {
		ezviz := models.EZVIZResponseStatus{}
		err = xml.Unmarshal([]byte(body), &ezviz)
		if err != nil {
			return nil, err
		}

		return &ezviz, nil
	} else {
		return nil, errors.New(fmt.Sprintf("Error %d", resp.StatusCode))
	}
}

func SetVerificationCode(username string, password string, address string, code string) (*models.EZVIZResponseStatus, error) {
	t := NewTransport(username, password)
	uri := "http://" + address + "/ISAPI/System/Network/EZVIZ"

	payload := `<?xml version="1.0" encoding="utf-8"?>
				<EZVIZ version="2.0" xmlns="http://www.isapi.org/ver20/XMLSchema">
					<enabled>true</enabled>
					<verificationCode>` + code + `</verificationCode>
				</EZVIZ>`

	resp, err := t.Put("PUT", uri, payload)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)

	fmt.Println(string(body))

	if resp.StatusCode == 200 {
		ezviz := models.EZVIZResponseStatus{}
		err = xml.Unmarshal([]byte(body), &ezviz)
		if err != nil {
			return nil, err
		}

		return &ezviz, nil
	} else {
		return nil, errors.New(fmt.Sprintf("Error %d", resp.StatusCode))
	}
}

func GetEZVIZNoAuth(address string) (*models.EZVIZ, error) {

	uri := "http://" + address + "/ISAPI/System/Network/EZVIZ"

	resp, err := http.Get(uri)

	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))

	if resp.StatusCode == 200 {
		ezviz := models.EZVIZ{}
		err = xml.Unmarshal([]byte(body), &ezviz)
		if err != nil {
			return nil, err
		}

		return &ezviz, nil
	} else {
		return nil, errors.New(fmt.Sprintf("Error %d", resp.StatusCode))
	}
}

func EnabledEZVIZNoAuth(address string, enable string) (*models.EZVIZResponseStatus, error) {
	uri := "http://" + address + "/ISAPI/System/Network/EZVIZ"

	payload := `<?xml version="1.0" encoding="utf-8"?>
				<EZVIZ version="2.0" xmlns="http://www.isapi.org/ver20/XMLSchema">
					<enabled>` + enable + `</enabled>
				</EZVIZ>`

	resp, err := http.Post(uri, "application/xml", strings.NewReader(payload))
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)

	fmt.Println(string(body))

	if resp.StatusCode == 200 {
		ezviz := models.EZVIZResponseStatus{}
		err = xml.Unmarshal([]byte(body), &ezviz)
		if err != nil {
			return nil, err
		}

		return &ezviz, nil
	} else {
		return nil, errors.New(fmt.Sprintf("Error %d", resp.StatusCode))
	}
}

func SetVerificationCodeNoAuth(address string, code string) (*models.EZVIZResponseStatus, error) {
	uri := "http://" + address + "/ISAPI/System/Network/EZVIZ"

	payload := `<?xml version="1.0" encoding="utf-8"?>
				<EZVIZ version="2.0" xmlns="http://www.isapi.org/ver20/XMLSchema">
					<enabled>true</enabled>
					<verificationCode>` + code + `</verificationCode>
				</EZVIZ>`

	resp, err := http.Post(uri, "application/xml", strings.NewReader(payload))
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)

	fmt.Println(string(body))

	if resp.StatusCode == 200 {
		ezviz := models.EZVIZResponseStatus{}
		err = xml.Unmarshal([]byte(body), &ezviz)
		if err != nil {
			return nil, err
		}

		return &ezviz, nil
	} else {
		return nil, errors.New(fmt.Sprintf("Error %d", resp.StatusCode))
	}
}
